package sd.rmi.simple.client;

import java.rmi.AccessException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import sd.rmi.simple.interfaces.IServerObject;
import sd.rmi.simple.server.Person;

public class ClientProxy implements IServerObject {
	private IServerObject object = null;
	private String url = null;
	private String service = null;
	
	public ClientProxy (String url, String service) {
		this.url = url;
		this.service = service;
		
		invokeRemoteServices();
	}
	
	public void invokeRemoteServices () {
		try {
			Registry reg = LocateRegistry.getRegistry( url );
			object = (IServerObject) reg.lookup( service );
		} catch (AccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	} 
	
	@Override
	public Person getPerson()  {
		try {
			return object.getPerson();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public String sayHello()  {
		try {
			return object.sayHello();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
